package user_handler

import (
	"fmt"
	"net/http"

	"bird/internal/api/service/user_service"
	"bird/internal/pkg/code"
	"bird/internal/pkg/core"
	"bird/internal/pkg/validation"
	"bird/pkg/errno"
)

type userRequest struct {
	PhoneNo              string `form:"phone_no" binding:"required"`
	RegisterType         int    `form:"register_type" binding:"required"`
	ProfilePhotoUrl      string `form:"profile_photo_url"`
	UserName             string `form:"user_name"`
	Gender               int    `form:"gender"`
	IdCardUrl            string `form:"id_card_url"`
	Birth                int64  `form:"birth"`
	Recommenders         string `form:"recommenders"`
	VehLicenseUrl        string `form:"veh_license_url"`
	VehLicenseExpire     int64  `form:"veh_license_expire"`
	DriverLicenseUrl     string `form:"driver_license_url"`
	DriverLicenseExpire  int64  `form:"driver_license_expire"`
	InsuranceUrl         string `form:"insurance_url"`
	InsuranceExpire      int64  `form:"insurance_expire"`
	QualificationCertUrl string `form:"qualification_cert_url"`
}

type userResponse struct {
	Id int32 `json:"id"`
}

// User 用户注册 && 注销
// @Summary 用户注册 && 注销
// @Description 用户注册 && 注销
// @Tags API.user
// @Accept json
// @Produce json
// @Param Request body userRequest true "请求信息"
// @Success 200 {object} userResponse
// @Failure 400 {object} code.Failure
// @Router /bird/api/user  [post]
func (h *handler) User() core.HandlerFunc {
	return func(c core.Context) {
		req := new(userRequest)
		res := new(userResponse)
		if err := c.ShouldBindForm(req); err != nil {
			c.AbortWithError(errno.NewError(
				http.StatusBadRequest,
				code.ParamBindError,
				validation.Error(err)).WithErr(err),
			)
			return
		}

		registerData := new(user_service.RegisterData)
		registerData.PhoneNo = req.PhoneNo
		registerData.RegisterType = req.RegisterType
		registerData.ProfilePhotoUrl = req.ProfilePhotoUrl
		registerData.UserName = req.UserName
		registerData.Gender = req.Gender
		registerData.IdCardUrl = req.IdCardUrl
		registerData.Birth = req.Birth
		registerData.Recommenders = req.Recommenders
		registerData.VehLicenseUrl = req.VehLicenseUrl
		registerData.VehLicenseExpire = req.VehLicenseExpire
		registerData.DriverLicenseUrl = req.DriverLicenseUrl
		registerData.DriverLicenseExpire = req.DriverLicenseExpire
		registerData.InsuranceExpire = req.InsuranceExpire
		registerData.InsuranceUrl = req.InsuranceUrl
		registerData.QualificationCertUrl = req.QualificationCertUrl

		var id int32
		var err error
		switch registerData.RegisterType {
		case 0:
			id, err = h.userService.RegisterDriver(c, registerData)
		case 1:
			id, err = h.userService.RegisterBasic(c, registerData)
		case 2:
			err = h.userService.BanUser(c, registerData.PhoneNo)
		default:
			err = fmt.Errorf("unknown action")
		}

		if err != nil {
			c.AbortWithError(errno.NewError(
				http.StatusInternalServerError,
				code.ServerError,
				code.Text(code.ServerError)).WithErr(err),
			)
			return
		}

		res.Id = id
		c.Payload(res)
	}
}
